package com.atguigu.lease.web.admin.controller.user;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.UserInfoService;
import com.atguigu.lease.web.admin.vo.user.UserInfoQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户信息管理")
@RestController
@RequestMapping("/admin/user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Operation(summary = "分页查询用户信息")
    @GetMapping("page")
    public Result<IPage<UserInfo>> pageUserInfo(@RequestParam long current, @RequestParam long size, UserInfoQueryVo queryVo) {
        Page<UserInfo> page=new Page<>(current,size);
        LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
        //优雅地使用动态条件，使用!ObjectUtils.isEmpty()代替queryVo.getPhone()!=null
        userInfoLambdaQueryWrapper.like(!ObjectUtils.isEmpty(queryVo.getPhone()),UserInfo::getPhone,queryVo.getPhone());
        userInfoLambdaQueryWrapper.eq(!ObjectUtils.isEmpty(queryVo.getStatus()),UserInfo::getStatus,queryVo.getStatus());
        userInfoService.page(page,userInfoLambdaQueryWrapper);
        return Result.ok(page);
    }

    /**
     * 根据用户id更新账号状态
     * @param id
     * @param status
     * @return
     */
    @Operation(summary = "根据用户id更新账号状态")
    @PostMapping("updateStatusById")
    public Result updateStatusById(@RequestParam Long id, @RequestParam BaseStatus status) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setStatus(status);
        userInfoService.updateById(userInfo);
        return Result.ok();
    }
}
